<template>
  <div class="data_list">
    <!-- 按钮 -->
    <div class="btns">
      <el-button size="small" type="primary" @click="toAdd">上报疫情</el-button>
    </div>
    <!-- 列表 -->
    <el-table :data="epidemics.list" size="small">
      <el-table-column label="序号" prop="id" align="center" width="90"/>
      <!-- <el-table-column label="序号" type="index" :index="1" /> -->
      <el-table-column label="国" prop="country" width="120" align="center"/>
      <el-table-column label="省" prop="province" width="120" align="center"/>
      <el-table-column label="市" prop="city" width="120" align="center" />
      <el-table-column label="区" prop="area" width="120" align="center"/>
      <el-table-column label="新增确诊" prop="confirmed" align="center" width="100" />
      <el-table-column label="疑似" align="center" width="100">
      <template slot-scope="scope">
          <el-tag type="warning" size="mini">{{ scope.row.suspected }}</el-tag>
      </template>
      </el-table-column>
      <el-table-column label="死亡" align="center" width="100">
        <template slot-scope="scope">
          <el-tag type="info" size="mini">{{ scope.row.dead }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="治愈" align="center" width="100">
        <template slot-scope="scope">
          <el-tag type="success" size="mini">{{ scope.row.cure }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="重症" align="center" width="100">
        <template slot-scope="scope">
          <el-tag type="danger" size="mini">{{ scope.row.severe }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="境外输入" prop="outside" align="center" width="100" />

      <el-table-column label="上报时间" align="center" >
         <template v-slot:default="scope">
          {{ scope.row.inputTime | formatDate }}
        </template> 
       </el-table-column>

      <!-- 删除按钮 -->
      <el-table-column label="操作" width="150" align="center">
        <!-- 作用域插槽 -->
        <template slot-scope="scope">
          <el-button @click="toDelete(scope.row)" type="danger" size="mini">删除</el-button>
        </template>
      </el-table-column>

    </el-table>
    <!-- 列表 -->

    <!-- 分页 -->
    <el-pagination
      background
      style="text-align: center"
      layout="prev, pager, next"
      :current-page="param.page"
      :page-size="param.pageSize"
      :total="epidemics.total"
      @current-change="currentChangeHandler">
    </el-pagination>
    <!-- /分页 -->


  
  </div>
</template>

<script>
import { get } from '@/utils/request'
import { formatDate } from '@/date.js'; // 在组件中引用date.js

export default {
    filters: {
    // 时间格式自定义 只需把字符串里面的改成自己所需的格式
    formatDate(time) {
      let date = new Date(time);
      return formatDate(date, 'yyyy年MM月dd日 hh:mm:ss');
    }
    },
  
  data() {
    return {
      param: {
        page: 1,
        pageSize: 10
      },
      epidemics: {}
    }
  },
  methods: {
    //删除疫情数据
      toDelete(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        //alert(JSON.stringify(row)); 将json对象转换成为字符串
        let url = "http://47.110.140.218:8001/epidemic/deleteById";
        get(url, { id: row.id }).then((resp) => {
          // 提示删除成功
          this.$message({ type: "success", message: resp.message });
          // 重新加载数据
          this.loadEpidemicData();
          });
        });
      },



    // 加载所有疫情数据
    loadEpidemicData() {
      let url = 'http://47.110.140.218:8001/epidemic/pageQuery'
      get(url, this.param).then(response => {
        this.epidemics = response.data
      })
    },
    // 去上报
    toAdd() {
      this.$router.push({
        path: '/data/input'
      })
    },
    currentChangeHandler(page) {
      this.param.page = page
      this.loadEpidemicData()
    }
  },
  mounted(){
      this.loadEpidemicData();
  }
}
</script>
